package model;

import java.util.ArrayList;
import java.util.List;

public class CourseSystem {
    private List<Course> allCourses;  // 所有课程
    private List<Student> students;   // 所有学生
    private List<Teacher> teachers;   // 所有老师

    public CourseSystem() {
        allCourses = new ArrayList<>();
        students = new ArrayList<>();
        teachers = new ArrayList<>();
        initData();  // 初始化测试数据
    }

    // 初始化测试数据（包含性别信息）
    private void initData() {
        // 创建老师（带性别）
        Teacher t1 = new Teacher("T001", "张世博", "男");
        Teacher t2 = new Teacher("T002", "张秀芬", "女");
        Teacher t3 = new Teacher("T003", "余有名", "男");
        Teacher t4 = new Teacher("T004", "朴星亮", "男");
        Teacher t5 = new Teacher("T005", "郭丹曦", "女");
        teachers.add(t1);
        teachers.add(t2);
        teachers.add(t3);
        teachers.add(t4);
        teachers.add(t5);

// 创建课程并关联老师
        Course c1 = new Course("C001", "Java编程", 2, t1, "周四 1-2节 | 主楼2501");
        Course c2 = new Course("C002", "太极拳", 3, t2, "周三3-4节 | 主校区操场");
        Course c3 = new Course("C003", "Linux", 2, t3, "周四 1-2节 | 综合实验楼0921");
        Course c4 = new Course("C004", "大学物理上", 4, t4, "周一 7-8节 | 主楼1705");
        Course c5 = new Course("C005", "太极剑", 3, t2, "周四 3-4节 | 主校区操场");
        Course c6 = new Course("C006", "大学物理下", 4, t4, "周二 7-8节 | 主楼1705");
        Course c7 = new Course("C007", "大学语文", 2, t5, "周四 7-8节 | 教学楼302");
        Course c8 = new Course("C008", "红楼梦", 2, t5, "周五 7-8节 | 教学楼302");
        allCourses.add(c1);
        allCourses.add(c2);
        allCourses.add(c3);
        allCourses.add(c4);
        allCourses.add(c5);
        allCourses.add(c6);
        allCourses.add(c7);
        allCourses.add(c8);

// 关联老师与课程
        t1.addCourse(c1);
        t2.addCourse(c2);
        t2.addCourse(c5);
        t3.addCourse(c3);
        t4.addCourse(c4);
        t4.addCourse(c6);
        t5.addCourse(c7);
        t5.addCourse(c8);

// 创建学生（带性别）
        students.add(new Student("S001", "LZL", "男"));
        students.add(new Student("S002", "LZB", "女"));
        students.add(new Student("S003", "SKY", "男"));

    }

    // 根据ID查找学生
    public Student findStudent(String studentId) {
        for (Student s : students) {
            if (s.getId().equals(studentId)) {
                return s;
            }
        }
        return null;
    }

    // 根据ID查找老师（新增方法）
    public Teacher findTeacher(String teacherId) {
        for (Teacher t : teachers) {
            if (t.getId().equals(teacherId)) {
                return t;
            }
        }
        return null;
    }

    // 根据课程号查找课程
    public Course findCourse(String courseId) {
        for (Course c : allCourses) {
            if (c.getCourseId().equals(courseId)) {
                return c;
            }
        }
        return null;
    }

    // 显示所有可选课程（含老师信息及性别）
    public void showAllCourses() {
        System.out.println("\n=== 可选课程列表 ===");
        for (Course course : allCourses) {
            System.out.println(course);
        }
    }

    // 学生选课操作
    public boolean selectCourse(Student student, String courseId) {
        Course course = findCourse(courseId);
        if (course == null) {
            System.out.println("错误：课程不存在！");
            return false;
        }
        return student.selectCourse(course);
    }

    // 学生退课操作
    public boolean dropCourse(Student student, String courseId) {
        Course course = findCourse(courseId);
        if (course == null) {
            System.out.println("错误：课程不存在！");
            return false;
        }
        return student.dropCourse(course);
    }

    // 显示学生课表
    public void showStudentSchedule(Student student) {
        List<Course> courses = student.getSelectedCourses();
        if (courses.isEmpty()) {
            System.out.println("\n=== 你的课表为空 ===");
            return;
        }
        System.out.println("\n=== " + student.getName() + "（" + student.getGender() + "）的课表 ===");
        for (Course course : courses) {
            System.out.println(course);
        }
    }

    // 显示老师课表（新增方法）
    public void showTeacherSchedule(Teacher teacher) {
        List<Course> courses = teacher.getCoursesTaught();
        if (courses.isEmpty()) {
            System.out.println("\n=== 你暂无授课课程 ===");
            return;
        }
        System.out.println("\n=== " + teacher.getName() + "（" + teacher.getGender() + "）的授课课表 ===");
        for (Course course : courses) {
            System.out.println(course);
        }
    }
}
